package 实验;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import java.io.*;
public class execute {
	public static class execute1 implements Serializable{
		private static final long serialVersionUID = 1L;
		private String name;
		private int id;
		private String gender;
		private String place;
		private String time;
		private String teacher;
		private String name1;
		
		public execute1(String name,int id,String gender,String place,String time,String teacher,String name1) {
			this.id=id;
			this.name=name;
			this.gender=gender;
			this.place=place;
			this.name1=name1;
			this.teacher=teacher;
			this.time=time;
		}
		public String toString() {
			return String.format("execute1.toString(): id=%d,name=%s,gender=%s,place=%s,name1=%s,teacher=%s,time=%s",id,name,gender,place,name1,teacher,time);
		
		}
	}
	public static void main(String[] args) {
		Class class0 = new Class(001,"线性代数","综合楼805","上午八点到九点四十");
        Class class1 = new Class(002,"离散数学","教学楼101","下午一点半到三点十五");
        Class class2 = new Class(003,"java语言","教学楼201","上午十点到十一四十");
        Teachers tea0 = new Teachers(001,"张老师","男",new Class[] {class2});
        Teachers tea1 = new Teachers(002,"魏老师","女",new Class[] {class1});
        Teachers tea2 = new Teachers(003,"庄老师","男",new Class[] {class0});
		Students stu0 = new Students(2020310880,"阿美","计算机","女",new Class[] {class0,class1,class2},new Teachers[] {tea2,tea1,tea0});
        Students stu1 = new Students(2020310881,"李四","计算机","男",new Class[] {class0,class1},new Teachers[] {tea2,tea1});
        Students stu2 = new Students(2020310882,"小芳","物联网","女",new Class[] {class1,class2},new Teachers[] {tea1,tea0});
        Students stu3 = new Students(2020310883,"张三","物联网","男",new Class[] {class0},new Teachers[] {tea2});

        System.out.println("学生编号:"+stu0.id+
                "\n学生姓名:"+stu0.name+
                "\n学生专业:"+stu0.major+
                "\n学生性别:"+stu0.sex);

        for(int i = 0; i<stu0.classes.length; i++){
            System.out.println("课程编号:"+stu0.classes[i].id+"\t课程名称:"+stu0.classes[i].name+"\t上课时间:"
                    +stu0.classes[i].time+"\t上课地点:"+stu0.classes[i].place+"\t教师:"+stu0.teachers[i].name);
        }

        System.out.println("学生编号:"+stu1.id+
                "\n学生姓名:"+stu1.name+
                "\n学生专业:"+stu1.major+
                "\n学生性别:"+stu1.sex);

        for(int i = 0; i<stu1.classes.length; i++){
            System.out.println("课程编号:"+stu1.classes[i].id+"\t课程名称:"+stu1.classes[i].name+"\t上课时间:"
                    +stu1.classes[i].time+"\t上课地点:"+stu1.classes[i].place+"\t教师:"+stu1.teachers[i].name);
        }
        System.out.println("学生编号:"+stu2.id+
                "\n学生姓名:"+stu2.name+
                "\n学生专业:"+stu2.major+
                "\n学生性别:"+stu2.sex);

        for(int i = 0; i<stu2.classes.length; i++){
            System.out.println("课程编号:"+stu2.classes[i].id+"\t课程名称:"+stu2.classes[i].name+"\t上课时间:"
                    +stu2.classes[i].time+"\t上课地点:"+stu2.classes[i].place+"\t教师:"+stu2.teachers[i].name);
        }
        System.out.println("学生编号:"+stu3.id+
                "\n学生姓名:"+stu3.name+
                "\n学生专业:"+stu3.major+
                "\n学生性别:"+stu3.sex);

        System.out.println("课程编号:"+stu3.classes[0].id+"\t课程名称:"+stu3.classes[0].name+"\t上课时间:"
                    +stu3.classes[0].time+"\t上课地点:"+stu3.classes[0].place+"\t教师:"+stu3.teachers[0].name);
        
	    System.out.println("退课后，学生选课信息");
	    try (Scanner scan = new Scanner(System.in)) {
			int d1 = scan.nextInt();
			if(d1 == 1) {
			String[] strArr= {stu0.classes[0].name, stu0.classes[1].name, stu0.classes[2].name};
			List<String> strList = Arrays.asList(strArr);
			List<String> arrayList = new ArrayList<String>(strList);
			if(arrayList.contains("线性代数"))
			{
				arrayList.remove("线性代数");
			}
			System.out.println("阿美剩余的课程为"+arrayList.toString());
			}
			
	    }
	    int id=stu3.id;
	    String gender=stu3.sex;
        String name=stu3.name;
        String name1=stu3.classes[0].name;
        String time=stu3.classes[0].time;
        String place=stu3.classes[0].place;
        String teacher=stu3.teachers[0].name;
	    execute1 b  = new execute1(name,id,gender,place,time,teacher,name1);
        System.out.println("序列化前："+b.toString());
        ObjectOutputStream outStream;
        ObjectInputStream inStream;
        String filePath = "D:/demo.txt";
        try {
            //创建一个ObjectOutputStream输出流
            outStream = new ObjectOutputStream(new FileOutputStream(filePath));
            //将对象序列化到文件filePath
            outStream.writeObject(b);
 
            inStream = new ObjectInputStream(new FileInputStream(filePath));
            execute1 readObject = (execute1)inStream.readObject();
            System.out.println("反序列化后："+readObject.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//	    FileOutputStream fop = null;
//	    File file;
//	    String s1 = "";
//	    StringBuffer s2 = new StringBuffer(s1);
//        for(int i = 0; i<stu0.classes.length; i++)
//        {
//        s2.insert(0, "课程编号:"+stu0.classes[i].id+"\t课程名称:"+stu0.classes[i].name+"\t上课时间:"
//                    +stu0.classes[i].time+"\t上课地点:"+stu0.classes[i].place+"\t教师:"+stu0.teachers[i].name);
//        }
//        for(int i = 0; i<stu1.classes.length; i++){
//            s2.insert(1,"课程编号:"+stu1.classes[i].id+"\t课程名称:"+stu1.classes[i].name+"\t上课时间:"
//                    +stu1.classes[i].time+"\t上课地点:"+stu1.classes[i].place+"\t教师:"+stu1.teachers[i].name);
//        }
//        s2.insert(2,"课程编号:"+stu3.classes[0].id+"\t课程名称:"+stu3.classes[0].name+"\t上课时间:"
//                +stu3.classes[0].time+"\t上课地点:"+stu3.classes[0].place+"\t教师:"+stu3.teachers[0].name);
//        String content = s2.toString() ;
//	    try {
//	    file = new File("D:/test.txt");//初始化file
//	    fop = new FileOutputStream(file);//初始化输出流
//	    // 若文件不存在,则创建它
//	    if (!file.exists()) {
//	    file.createNewFile();
//	    }
//	    // 获取字节的内容数组
//	    byte[] contentInBytes = content.getBytes(); 
//	    fop.write(contentInBytes);//写出到指定路径文件中字节的内容数组
//	    fop.flush();
//	    fop.close();
//	    System.out.println("Done");
//	    } catch (IOException e) { //捕捉异常
//	    e.printStackTrace();
//	    } finally {
//	    try {
//	    if (fop != null) {
//	    fop.close();
//	    }
//	    } catch (IOException e) { //捕捉异常
//	    e.printStackTrace();
//	    }
//	    }
	    
	    

	}
	
	}